package com.longsunhd.yum.buffalo.activity;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.widget.LinearLayout;

import com.longsunhd.yum.buffalo.R;
import com.longsunhd.yum.buffalo.base.ControlsActivity;
import com.longsunhd.yum.buffalo.base.GetTokenUtil;
import com.longsunhd.yum.buffalo.bean.GetMedicationFieldBean;
import com.longsunhd.yum.buffalo.network.action.MedicateApiUtils;
import com.longsunhd.yum.buffalo.util.generator.UIGenerator;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.longsunhd.yum.buffalo.constant.Constant.BuffaloId;

/**
 * 作者：黄宣淇
 * 时间：2021/5/10 0010
 * 如何代码没有备注，一个月后，只有你和上帝知道--鲁迅
 */
public class MedicationActivity extends ControlsActivity {
    @BindView(R.id.mll_content)
    public LinearLayout mll_content;

    @Override
    protected int getContentView() {
        return R.layout.activity_medicate;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
    }


    private int buffaloID=0;//牛的编号
    @Override
    protected boolean initBundle(Bundle bundle) {
        if(bundle!=null){
            buffaloID=  bundle.getInt(BuffaloId,0);
        }
        return super.initBundle(bundle);

    }

    @Override
    protected void initData() {
        super.initData();
        final String token = GetTokenUtil.getToken(this);
        if (TextUtils.isEmpty(token)) {
            return;
        }
        MedicateApiUtils.newInstance().getMedicate(token, "", new Handler() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                GetMedicationFieldBean getMedicationFieldBean = (GetMedicationFieldBean) msg.obj;
                mTvTitle.setText(getMedicationFieldBean.getData().getTitle());
                List<GetMedicationFieldBean.DataBean.FormBean> list = getMedicationFieldBean.getData().getForm();
                for (GetMedicationFieldBean.DataBean.FormBean formBean : list) {
                    UIGenerator.GenerateEditDetailUi(MedicationActivity.this,token,formBean,mll_content);
                }

            }
        });
    }

    @OnClick(R.id.mBtSubmit)
    public void mBtSubmit() {
        //提交编辑
        hashMap.put("buffalo_id",buffaloID+"");//重新提交牛id
        UIGenerator.SumbitAction(this);//给hashmap赋值
        if (hashMap != null) {
            final String token = GetTokenUtil.getToken(this);
            MedicateApiUtils.newInstance().postMedicate(token, hashMap, new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    finish();
                }
            });
        }
    }
}
